/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.bsplus.util.security;

import javax.annotation.Resource;
import org.bsplus.domain.User;
import org.bsplus.service.UserService;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 *
 * @author sam
 */
public class UserDetailsServiceImpl implements UserDetailsService
{
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username)
    {
        User user = userService.getUser(username);
        if(user == null)
        {
            throw new UsernameNotFoundException("User '" + username + "' not found");
        }
        return new UserDetailsImpl(user);
    }

    @Resource
    @Required
    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }
}
